﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using System.IO;
using System.Reflection;

using System.Xml.Serialization;

namespace Winz.FoeBot
{
    public class BotSettings
    {

        public string test { get; set; }
        public string url { get; set; }

        public int supplyDuration { get; set; }
        public int goodsDuration { get; set; }

        public int checkEvery { get; set; }
        public int checkEveryRandomizePercent { get; set; }


        public bool waitForUserSelectMotivation { get; set; }
        public bool useDefaultIfNotSet { get; set; }
        public int waitForUserSelectMotivationDelay { get; set; }

        public bool AutomaticStartProductionBot { get; set; }
        
        public bool AutomaticReload { get; set; }
        public int AutomaticReloadDelay { get; set; }


        public Point originMapPoint { get; set; }
        public int xDelta { get; set; }
        public int yDelta { get; set; }
        public Point motivateBtn { get; set; }
        public Point polishBtn { get; set; }
        public Point bluePrintFoundCloseBtn { get; set; }
        public Point closeEventsBtn { get; set; }
        public Point closeNewEventBtn { get; set; }


        public Point socialPlayerOrigin { get; set; }
        public Point socialPlayerDelta { get; set; }
        public Point socialNext { get; set; }
        public Point socialPrevious { get; set; }
        public Point socialLast { get; set; }
        public Point socialFirst { get; set; }
        public Point socialFriends { get; set; }
        public Point socialGuild { get; set; }
        public Point socialNeighbours { get; set; }
        public MotivationOrdersType defaultMotivationOrder { get; set; }


        public bool visitNeighboors { get; set; }
        public bool visitFriends { get; set; }
        public bool visitGuild { get; set; }

        public string folderBmp {get;set;}
        public string folderScreenShots { get; set; }
        public string fileMotivationPrefs { get; set; }

        public string lastAdress { get; set; }



        public BotSettings()
        {
            url = "fr.forgeofempires.com";

            supplyDuration = 1;
            goodsDuration = 1;

            checkEvery = 5;
            checkEveryRandomizePercent = 20;

            defaultMotivationOrder = MotivationOrdersType.PO_MM_MO;

            waitForUserSelectMotivation = true;
            useDefaultIfNotSet = true;
            waitForUserSelectMotivationDelay = 10;

            AutomaticStartProductionBot = true;
            AutomaticReload = true;
            AutomaticReloadDelay = 120;
            
            visitNeighboors = true;
            visitGuild = true;
            visitFriends = true;


            originMapPoint = new Point(1950, -45);
            xDelta = 52;
            yDelta = 26;
            motivateBtn = new Point(52, 1645);
            polishBtn = new Point(160, 1645);
            closeEventsBtn = new Point(1994, 1275);
            closeNewEventBtn = new Point(2005 ,1096 );


            socialPlayerOrigin = new Point(180, 1940);
            socialPlayerDelta = new Point(110, 0);
            socialNext = new Point(913, 1934);
            socialPrevious = new Point(245, 1935);
            socialLast = new Point(912, 1968);
            socialFirst = new Point(244, 1967);
            socialFriends = new Point(866, 1853);
            socialGuild = new Point(803, 1854);
            socialNeighbours = new Point(738, 1856);
            bluePrintFoundCloseBtn = new Point(2095, 1210);

            folderBmp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\img";
            folderScreenShots = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\FoE\img";
            fileMotivationPrefs = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\FoE\MotivationsPref.xml";

            lastAdress = "";
        }



        public void Save(string path = null)
        {
            if (path == null)
                path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\FoE\Settings.xml"; 
            TextWriter writer = null;
            XmlSerializer ser = new XmlSerializer(typeof(BotSettings));
            try
            {
                writer = new StreamWriter(path);
            }
            catch (DirectoryNotFoundException e)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));
                writer = new StreamWriter(path);
            }
            finally
            {
                using (writer)
                {
                    ser.Serialize(writer, this);
                    writer.Close();
                }
            }
        }

        public static BotSettings Load(string path = null)
        {
            if (path == null)
                path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\FoE\Settings.xml";

            BotSettings settings;
            XmlSerializer ser = null;
            try
            {
                ser = new XmlSerializer(typeof(BotSettings));
            }
            catch (Exception e)
            {
            }
            
            TextReader reader = null;
            try
            {
                reader = new StreamReader(path);
                settings = (BotSettings)ser.Deserialize(reader);
                reader.Close();
            }
            catch (FileNotFoundException e)
            {
                settings = new BotSettings();
            }
            catch (DirectoryNotFoundException e)
            {
                settings = new BotSettings();
            }

            return settings;
        }

    }

}
